<?php
Loader::loadFile('config.php', "modules/InnoForum");

Loader::loadClass('DateUtilEx', "modules/InnoForum/pnincludes");
Loader::loadClass('ObjectUtilEx', "modules/InnoForum/pnincludes");
Loader::loadClass('PNObjectEx', "modules/InnoForum/pnincludes");
Loader::loadClass('PNObjectExArray', "modules/InnoForum/pnincludes");
Loader::loadClass('DBUtilEx', "modules/InnoForum/pnincludes");
Loader::loadClass('SecurityUtilEx', "modules/InnoForum/pnincludes");
Loader::loadClass('InnoUtil', "modules/InnoForum/pnincludes");

/**
 * initialise block
 * 
 * @author       The PostNuke Development Team
 */
function InnoForum_VarifyUserTopicsblock_init()
{

}

/**
 * get information on block
 * 
 * @author       The PostNuke Development Team
 * @return       array       The block information
 */
function InnoForum_VarifyUserTopicsblock_info()
{
    return array('text_type'      => 'Varified User Topics',
                 'module'         => 'InnoForum',
                 'text_type_long' => 'Show Varified User Topics of InnoForum',
                 'allow_multiple' => true,
                 'form_content'   => false,
                 'form_refresh'   => false,
                 'show_preview'   => true);
}

/**
 * display block
 * 
 * @author       The PostNuke Development Team
 * @param        array       $blockinfo     a blockinfo structure
 * @return       output      the rendered bock
 */
function InnoForum_VarifyUserTopicsblock_display($blockinfo)
{

    $modname  = 'InnoForum';
    $render = pnRender::getInstance($modname);

    $vars = pnBlockVarsFromContent($blockinfo['content']);
    $pagesize = isset($vars['pagesize'])?$vars['pagesize']:20;

    $pntables = pnDBGetTables();
    //print_r($pntables); exit;
    //Query topics
    $tableInnoforumTopics  = $pntables['innoforum_topics'];
    $columnInnoforumTopics = $pntables['innoforum_topics_column'];
    $tableGroupMembership  = $pntables['group_membership'];
    $columnGroupMembership = $pntables['group_membership_column'];

    $queryInnoforumTopics = "SELECT
                                $tableInnoforumTopics.$columnInnoforumTopics[id],
                                $tableInnoforumTopics.$columnInnoforumTopics[ref_id],
                                $tableInnoforumTopics.$columnInnoforumTopics[title],
                                $tableInnoforumTopics.$columnInnoforumTopics[type_id],
                                $tableInnoforumTopics.$columnInnoforumTopics[name],
                                $tableInnoforumTopics.$columnInnoforumTopics[price],
                                $tableInnoforumTopics.$columnInnoforumTopics[price_detail],
                                $tableInnoforumTopics.$columnInnoforumTopics[onsale],
                                $tableInnoforumTopics.$columnInnoforumTopics[province],
                                $tableInnoforumTopics.$columnInnoforumTopics[count_post],
                                $tableInnoforumTopics.$columnInnoforumTopics[count_view],
                                $tableInnoforumTopics.$columnInnoforumTopics[has_image],
                                $tableInnoforumTopics.$columnInnoforumTopics[update_icon],
                                $tableInnoforumTopics.$columnInnoforumTopics[status],
                                $tableInnoforumTopics.$columnInnoforumTopics[obj_status],
                                $tableInnoforumTopics.$columnInnoforumTopics[cr_uid],
                                $tableInnoforumTopics.$columnInnoforumTopics[cr_date],
                                IF ( DATEDIFF(DATE($tableInnoforumTopics.$columnInnoforumTopics[cr_date]),DATE(NOW())) <1 
                                    AND DATEDIFF(DATE($tableInnoforumTopics.$columnInnoforumTopics[cr_date]),DATE(NOW())) >-3,
                                    'T','F'
                                ) as 'is_new',
                                $tableGroupMembership.$columnGroupMembership[gid]
                              FROM
                                $tableInnoforumTopics,
                                $tableGroupMembership
                              WHERE
                                $tableGroupMembership.$columnGroupMembership[uid] = $tableInnoforumTopics.$columnInnoforumTopics[cr_uid]
                              AND 
                              (
                                  $tableGroupMembership.$columnGroupMembership[gid] = 5
                                OR
                                  $tableGroupMembership.$columnGroupMembership[gid] = 6
                                OR
                                  $tableGroupMembership.$columnGroupMembership[gid] = 7
                              )
                              ORDER BY 
                                $tableInnoforumTopics.$columnInnoforumTopics[obj_status],
                                $tableInnoforumTopics.$columnInnoforumTopics[temp_cr_date] 
                              DESC 
                              LIMIT ".$pagesize."
                        ";
    //print($queryInnoforumTopics); exit;
    $columnInnoforumTopics = array( "id",
                                    "ref_id",
                                    "title",
                                    "type_id",
                                    "name",
                                    "price",
                                    "price_detail",
                                    "onsale",
                                    "province",
                                    "count_post",
                                    "count_view",
                                    "has_image",
                                    "update_icon",
                                    "status",
                                    "obj_status",
                                    "cr_uid",
                                    "cr_date",
                                    "is_new",
                                    "gid"
                             );
    $resultInnoforumTopics = DBUtil::executeSQL($queryInnoforumTopics);
    $topicsArray = DBUtil::marshallObjects ($resultInnoforumTopics, $columnInnoforumTopics);
    //print($queryInnoforumTopics); exit;
    //print_r($topicsArray); exit;

    $render->assign('objectArray', $topicsArray);

    $blockinfo['content'] = $render->fetch('block_varifyusertopics.htm');
    return themesideblock($blockinfo);

/*
    $blockinfo['content'] = "hello";
    return themesideblock($blockinfo);
*/
}


/**
 * modify block settings
 * 
 * @author       The PostNuke Development Team
 * @param        array       $blockinfo     a blockinfo structure
 * @return       output      the bock form
 */
function InnoForum_VarifyUserTopicsblock_modify($blockinfo)
{
  $vars = pnBlockVarsFromContent($blockinfo['content']);
  //load render
  $render = pnRender::getInstance('InnoForum');
  $render->assign('form', $vars);
  
	return $render->fetch('block_varifyusertopics_modify.htm');
}


/**
 * update block settings
 * 
 * @author       The PostNuke Development Team
 * @param        array       $blockinfo     a blockinfo structure
 * @return       $blockinfo  the modified blockinfo structure
 */
function InnoForum_VarifyUserTopicsblock_update($blockinfo)
{
    
    // Get current content
    $vars = pnBlockVarsFromContent($blockinfo['content']);
    
    // alter the corresponding variable
    $form = FormUtil::getPassedValue ('form', false );
    $vars['pagesize'] = !empty($form['pagesize']) ? $form['pagesize'] : 10;
    $vars['thumb']    = !empty($form['thumb']) ? $form['thumb'] : 0;
    $vars['imagewidth']    = !empty($form['imagewidth']) ? $form['imagewidth'] : '80';
    $vars['imageheight']    = !empty($form['imageheight']) ? $form['imageheight'] : '60';
    $vars['imagewidth']    = (trim($form['imagewidth']) == 'auto') ? '' : $form['imagewidth'];
    $vars['imageheight']    = (trim($form['imageheight']) == 'auto') ? '' : $form['imageheight'];

    // write back the new contents
    $blockinfo['content'] = pnBlockVarsToContent($vars);

    // clear the block cache
    $pnRender = pnRender::getInstance('InnoForum');
    $pnRender->clear_cache('block_varifyusertopics.htm');
    
    return $blockinfo;
}
